package StudentAchievementManagementSystem.ui;

import StudentAchievementManagementSystem.component.AddStuGradeDialog;
import StudentAchievementManagementSystem.component.DeleteStuGradeDialog;
import StudentAchievementManagementSystem.component.UpdateStuGradeDialog;
import StudentAchievementManagementSystem.mysql.MysqlUtil;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

public class InforStatComponent extends Box{
    final int WIDTH=850;
    final int HEIGHT=600;

    JFrame jf = null;
    private JTable table;
    private Vector<String> titles;
    private Vector<Vector> tableData;
    private DefaultTableModel tableModel;

    public InforStatComponent(JFrame jf) throws SQLException, IOException, ClassNotFoundException {
        //垂直布局
        super(BoxLayout.Y_AXIS);
        //组装视图
        this.jf = jf;
        JPanel btnPanel = new JPanel();
        Color color = new Color(203,220,217);
        btnPanel.setBackground(color);
        btnPanel.setMaximumSize(new Dimension(WIDTH,80));
        btnPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

        JLabel snoLabel = new JLabel("学号");
        JTextField snoTextField = new JTextField(4);
        JLabel pwdLabel = new JLabel("密码");
        JTextField pwdTextField = new JTextField(4);
        JComboBox termComboBox = new JComboBox();
        termComboBox.addItem("1.1");
        termComboBox.addItem("1.2");
        termComboBox.addItem("2.1");
        termComboBox.addItem("2.2");
        termComboBox.addItem("3.1");
        termComboBox.addItem("3.2");
        termComboBox.addItem("4.1");
        termComboBox.addItem("4.2");

        JButton flushBtn = new JButton("刷新");

        flushBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String sno = snoTextField.getText();
                String pwd = pwdTextField.getText();
                String term=(String)termComboBox.getSelectedItem();
                try {
                    requestData(sno,pwd,term);
                } catch (SQLException ex) {
                    ex.printStackTrace();
                } catch (IOException ex) {
                    ex.printStackTrace();
                } catch (ClassNotFoundException ex) {
                    ex.printStackTrace();
                }
            }
        });

        btnPanel.add(flushBtn);

        this.add(btnPanel);

        //组装表格
        String[] ts = {"学号","课程号","姓名","专业","学科","分数"};
        titles = new Vector<>();
        for (String title : ts) {
            titles.add(title);
        }

        tableData = new Vector<>();

        tableModel = new DefaultTableModel(tableData,titles);
        table = new JTable(tableModel){
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
//        //设置只能选中一行
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        JScrollPane scrollPane = new JScrollPane(table);
        this.add(scrollPane);
    }

    //请求数据
    public void requestData(String sno,String pwd,String term) throws SQLException, IOException, ClassNotFoundException {
        Connection connection = MysqlUtil.getConnection();
        Statement statement = connection.createStatement();
        String sql = "select student.sno,course.cno,sname,major,course.cname,grade from student ,studentcoursegrade,course where student.sno=studentcoursegrade.sno and course.cno=studentcoursegrade.cno ";
        Vector<Vector> vectors = new Vector<Vector>();
        ResultSet resultSet = statement.executeQuery(sql);
        while(resultSet.next()){
            Vector<String> vector1 = new Vector < String > ();
            for (int i=1;i<7;i++){
                vector1.add(resultSet.getString(i));
            }
            try{
                vectors.add(vector1);
//                vector1.clear();
            }catch (NullPointerException e){
                e.printStackTrace();
            }
        }
        //清空tableData的数据
        tableData.clear();
        for (Vector vector : vectors) {
            tableData.add(vector);
        }
        //刷新表格
        tableModel.fireTableDataChanged();
    }
}